package com.example.common.utils;

import java.io.Serializable;
import java.util.StringJoiner;

public class ResponseResult <T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private Integer code;

    private String message;

    private T data;

    public ResponseResult(Integer code,String message){
        this.code = code;
        this.message = message;
    }

    public ResponseResult(Integer code,String message,T data){
        this(code,message);
        this.data = data;
    }

    public static <T> ResponseResult<T> success(){
        return new ResponseResult<>(ResponseCode.SUCCESS.getCode(),ResponseCode.SUCCESS.getMessage());
    }

    public static <T> ResponseResult<T> success(T data){
        return new ResponseResult<>(ResponseCode.SUCCESS.getCode(),ResponseCode.SUCCESS.getMessage(),data);
    }

    public static <T> ResponseResult<T> fail(){
        return new ResponseResult<>(ResponseCode.FAILURE.getCode(),ResponseCode.FAILURE.getMessage());
    }

    public static <T> ResponseResult<T> fail(Integer code,String message){
        return new ResponseResult<>(code,message);
    }

    public static <T> ResponseResult<T> error(){
        return new ResponseResult<>(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getMessage());
    }

    public void setCode(Integer code){
        this.code = code;
    }
    public void setMessage(String message){
        this.message = message;
    }
    public void setData(T data){
        this.data = data;
    }
    public Integer getCode(){
        return this.code;
    }
    public String getMessage(){
        return this.message;
    }
    public T getData(){
        return this.data;
    }

    @Override
    public String toString() {
        return (new StringJoiner(", ", ResponseResult.class.getSimpleName() + "[", "]"))
                .add("code=" + this.code)
                .add("message='" + this.message + "'")
                .add("data=" + this.data)
                .toString();
    }


}
